knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(GTFShift)
#> Warning: replacing previous import 'GTFSwizard::read_gtfs' by
#> 'tidytransit::read_gtfs' when loading 'GTFShift'
#> Warning: replacing previous import 'GTFSwizard::write_gtfs' by
#> 'tidytransit::write_gtfs' when loading 'GTFShift'

library(tidytransit)
library(mapview)
library(sf)
library(dplyr)

Introduction

Analyzing public transit feeds is important to understand its territorial coverage and dynamics, both on its spatial and temporal dimensions. GTFShift provides several methods that encapsulate pre-defined methodologies for them. This document explores their applicability with simple examples.

This article uses a GTFS feed from the library GTFS database for Portugal as an example. Refer to the vignette(“download”) for more details.

# Get GTFS from library GTFS database for Portugal
data <- read.csv(system.file("extdata", "gtfs_sources_pt.csv", package = "GTFShift"))
gtfs_id = "lisboa"
gtfs <- GTFShift::load_feed(data[data$ID==gtfs_id,]$URL)

Analyse hourly frequency per stop

To analyse frequencies at stops, use GTFShift::get_stop_hour_frequency(), producing, for each, an aggregated counting of bus servicing it per hour.

# Perform frequency analysis
frequencies_stop <- GTFShift::get_stop_frequency_hourly(gtfs)
summary(frequencies_stop)
#>    stop_id               hour         frequency               geometry    
#>  Length:39213       Min.   : 6.00   Min.   : 1.000   POINT        :39213  
#>  Class :character   1st Qu.:10.00   1st Qu.: 3.000   epsg:4326    :    0  
#>  Mode  :character   Median :14.00   Median : 6.000   +proj=long...:    0  
#>                     Mean   :14.21   Mean   : 7.689                        
#>                     3rd Qu.:18.00   3rd Qu.:10.000                        
#>                     Max.   :23.00   Max.   :44.000

Its returns an sf data.frame that can be displayed using mapview, or stored in GeoPackage format.

# Display map
mapview::mapview(frequencies_stop, zcol="frequency", legend=TRUE, cex=4, layer.name = "Frequency (hour)")